#!/usr/bin/python
import random
import time
import os
import yaml

class RandomVerse:
    menu_options = ["",'Verse', "Add Verse", "Delete Verse"]
    verse_list = {}
    def __init__(self):
        print ""
        print ""
        print "Welcome to a random verse generator!"
        time.sleep(1)

        def LoadVerses():
            try:
                with open("verse_list.yml", "r") as f:
                    self.verse_list = yaml.load(f.read())
                    print "Your verses have been loaded!"
            except:
                self.verse_list = {'John':{'3':{'16':"For God so loved the world,\
                 that he gave his only Son, that whoever believes in him\
                  should not perish but have eternal life.", 'notes':""}}}

        def SaveVerses():
            with open("verse_list.yml", "w") as f:
                yaml.dump(self.verse_list, f)
                f.close()
                print "Your verses have been saved!"
                print "     See you 'round!"

        def OpeningMenu():
            """The opening menu for the program, providing options for the user to choose
            to use features."""
            print ""
            print "What would you like to see?"

            for idx,option in enumerate(self.menu_options):
                if idx == 0:
                    pass
                else:
                    print str(idx)+": "+option
            option = raw_input("> ")
            if option == "1" or option == 'Verse':
                print "Which book would you like to open?"
                book = raw_input(": ").lower
                print "Which chapter is this verse in?"
                chapter = raw_input(": ").lower
                print "What verse would you like displayed?"
                print "(If you would like every verse for this chapter, press enter.)"
                verse = raw_input(": ").lower
                VerseLookup(book, chapter, verse)

        def VerseLookup(book, chapter, verse):
            book_list = []
            chapter_list = []
            verse_list = []
            for passage in self.verse_list.keys():
                print passage
                book_list.append(passage)
            if book not in book_list:
                print "That book has no entries!"
                OpeningMenu()
                time.sleep(1)
            else:
                for passage in self.verse_list[book].keys():
                    chapter_list.append(passage)
                if chapter not in chapter_list:
                    print "That chapter has no entries!"
                    time.sleep(1)
                    OpeningMenu()
                else:
                    for passage in self.verse_list[book][chapter].keys():
                        verse_list.append(passage)
                    if verse not in verse_list:
                        print "That verse does not seem to have been entered."
                        time.sleep(1)
                        OpeningMenu()
                    else:
                        print self.verse_list[book][chapter][verse]

        LoadVerses()
        OpeningMenu()
        SaveVerses()


if __name__ == "__main__":
    RandomVerse()
